package com.hsoft.zhxy.app.xtgl_02_01.service.impl;


import com.hsoft.zhxy.app.xtgl_02_01.mapper.JcggCzrzMapper;
import com.hsoft.zhxy.app.common.model.entity.JcggCzrz;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.hsoft.zhxy.app.xtgl_02_01.model.query.JcggCzrzQyery;
import com.hsoft.zhxy.app.xtgl_02_01.service.JcggCzrzService;
import com.hsoft.zhxy.common.base.BaseService;
import com.hsoft.zhxy.common.result.ResultBody;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

 /**
 * All rights Reserved, Designed By 哈尔滨工业大学软件工程股份有限公司
 * 
 * @Title: 操作日志 Service层接口实现类
 * @Package 
 * @Description: 
 * @author: system
 * @date: 2020-10-26
 */
@Log4j2
@Service
public class JcggCzrzServiceImpl extends BaseService implements JcggCzrzService {

    @Autowired
    private JcggCzrzMapper jcggCzrzMapper;


    /**
     * 查询单条数据
     * @param jcggCzrz
     * @return
     */
    public JSONObject findOne(JcggCzrz jcggCzrz) {
        JcggCzrz rs = jcggCzrzMapper.selectOne(new QueryWrapper<JcggCzrz>().setEntity(jcggCzrz));
        return ResultBody.setData(rs);
    }


    /**
     * 查询列表数据
     * @param jcggCzrz
     * @return
     */
    public JSONObject findList(JcggCzrz jcggCzrz) {
        List<JcggCzrz> list = jcggCzrzMapper.selectList(new QueryWrapper<JcggCzrz>().setEntity(jcggCzrz));
        return ResultBody.setData(list);
    }


    /**
     * 查询分页数据
     * @return
     */
    public JSONObject findListByPage(JcggCzrzQyery jcggCzrzQyery) {

        Page<JcggCzrz> pageObj = new Page<JcggCzrz>(jcggCzrzQyery.getPage(), jcggCzrzQyery.getPageSize());
        QueryWrapper<JcggCzrz> queryWrapper = new QueryWrapper<JcggCzrz>();

        if (jcggCzrzQyery.getSjfw().length > 0) {
            jcggCzrzQyery.setKssj(jcggCzrzQyery.getSjfw()[0]);
            jcggCzrzQyery.setJssj(jcggCzrzQyery.getSjfw()[1]);
        }
        queryWrapper.like(StringUtils.isNotBlank(jcggCzrzQyery.getWymbs()), "wymbs", jcggCzrzQyery.getWymbs())
                .like(StringUtils.isNotBlank(jcggCzrzQyery.getXyybs()), "xyybs", jcggCzrzQyery.getXyybs())
                .and(StringUtils.isNotBlank(jcggCzrzQyery.getGxhxm()),
                        wrapper -> wrapper.like(StringUtils.isNotBlank(jcggCzrzQyery.getGxhxm()), "czrid", jcggCzrzQyery.getGxhxm())
                                .or().like("czrxm", jcggCzrzQyery.getGxhxm()))
                .ge(StringUtils.isNotBlank(jcggCzrzQyery.getKssj()), "czsj", jcggCzrzQyery.getKssj())
                .le(StringUtils.isNotBlank(jcggCzrzQyery.getJssj()), "czsj", jcggCzrzQyery.getJssj())
                .orderByDesc("czsj");

        IPage<JcggCzrz> result = jcggCzrzMapper.selectPage(pageObj, queryWrapper);
        if(log.isDebugEnabled()) log.debug(result.getRecords());
        return ResultBody.setData(result.getTotal(), result.getRecords());
    }


    /**
     * 新增
     * @param jcggCzrz
     * @return
     */
    public JSONObject add(JcggCzrz jcggCzrz) {
        jcggCzrz.setCjsj(LocalDateTime.now());
        jcggCzrz.setCjr(getYhid());
        jcggCzrz.setBgsj(LocalDateTime.now());
        jcggCzrz.setBgr(getYhid());
        return ResultBody.setData(jcggCzrzMapper.insert(jcggCzrz));
    }


    /**
     * 修改
     * @param jcggCzrz
     * @return
     */
    public JSONObject update(JcggCzrz jcggCzrz) {
        jcggCzrz.setBgsj(LocalDateTime.now());
        jcggCzrz.setBgr(getYhid());
        return ResultBody.setData(jcggCzrzMapper.updateById(jcggCzrz));
    }


    /**
     * 删除
     * @param jcggCzrz
     * @return
     */
    public JSONObject delete(JcggCzrz jcggCzrz) {
        return ResultBody.setData(jcggCzrzMapper.delete(new QueryWrapper<JcggCzrz>().setEntity(jcggCzrz)));
    }


}
